const connection = require("../app/database");
const dayjs = require("dayjs");
const { v4: uuidv4 } = require("uuid");

class TagService {
  constructor() {
    this.formName = "tag";
  }
  // 新增
  async create(_params) {
    return new Promise(async (resolve, reject) => {
      try {
        const { text, userId } = _params;
        const id = uuidv4();
        const createTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
        const statement = `INSERT INTO ${this.formName} (id,text,user_id,createTime) VALUES (?,?,?,?);`;
        const res = await connection.execute(statement, [id, text, userId, createTime]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 批量删除
  async delete(_ids) {
    return new Promise(async (resolve, reject) => {
      try {
        console.log("_ids", _ids);
        const placeholders = _ids.map(() => "?").join(",");
        const statement = `DELETE FROM ${this.formName} WHERE id IN (${placeholders});`;
        console.log(statement);

        const [res] = await connection.execute(statement, _ids);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 分页
  async page(_params) {
    return new Promise(async (resolve, reject) => {
      const { pageNum, pageSize, text = "", userId } = _params;

      const offset = (pageNum - 1) * pageSize;
      const limit = pageSize;
      try {
        const statement = `
           SELECT * FROM ${this.formName}
           WHERE text LIKE ? AND user_id = ?
           LIMIT ${offset}, ${limit};
         `;
        const [res] = await connection.execute(statement, [`%${text}%`, userId]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 编辑
  async update(_params) {
    const { id, text } = _params;
    const updateTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `UPDATE ${this.formName} SET text = ?, updateTime = ? WHERE id = ?;`;
        const res = await connection.execute(statement, [text, updateTime, id]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 查询
  async findName(_params) {
    const { text, userId } = _params;
    console.log(text, userId);

    return new Promise(async (resolve, reject) => {
      try {
        const statement = `SELECT * FROM ${this.formName} WHERE text = ? && user_id = ?;`;
        const [res] = await connection.execute(statement, [text, userId]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 总数
  async allNum(_userId) {
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `SELECT COUNT(*) FROM ${this.formName} WHERE user_id = ?;`;
        const [res] = await connection.execute(statement, [_userId]);
        resolve(res[0]["COUNT(*)"]);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 全量搜索
  async select(_userId) {
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `
           SELECT * FROM ${this.formName} WHERE user_id = ?;
         `;
        const [res] = await connection.execute(statement, [_userId]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }

    // 详情
    async detail(_id) {
      return new Promise(async (resolve, reject) => {
        try {
          const statement = `SELECT * FROM ${this.formName} WHERE id = ?;`;
          const [res] = await connection.execute(statement, [_id]);
          resolve(res[0]);
        } catch (error) {
          reject(error);
        }
      });
    }
}

module.exports = new TagService();
